package com.start.admin.mappers;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.start.admin.entity.StartSysRoleDataRule;
import com.start.common.vo.SysMenuDataRulesVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface SysRoleDataRuleMapper extends BaseMapper<StartSysRoleDataRule> {
    String SYS_MENU_DATA_RULES_VO_SQL = "SELECT sm.component,  " +
            "    sm.dynamic_form_id,  " +
            "   smdr.*  " +
            "  FROM start_sys_role_data_rule srdr  " +
            "     INNER JOIN start_sys_user_role sur ON srdr.role_id = sur.role_id  " +
            "     INNER JOIN start_sys_role sr ON sr.id = sur.role_id  " +
            "     INNER JOIN start_sys_role_menu srm ON srm.role_id = sur.role_id  " +
            "     INNER JOIN start_sys_menu sm ON srm.menu_id = sm.id  " +
            "     INNER JOIN start_sys_menu_data_rules smdr on smdr.menu_id = sm.id  " +
            "AND srdr.data_rule_id = smdr.id  " +
            "AND srdr.menu_id = smdr.menu_id  " +
            "AND srdr.menu_id = srm.menu_id  " +
            "AND srdr.role_id = sur.role_id  ";

    @Select(SYS_MENU_DATA_RULES_VO_SQL + " AND sur.user_id = #{userId}")
    List<SysMenuDataRulesVo> listByUserId(@Param("userId") String userId);

    @Select(SYS_MENU_DATA_RULES_VO_SQL +
            " AND sur.user_id = #{userId} " +
            " AND sm.component=#{component} ")
    List<SysMenuDataRulesVo> listByUserIdAndComponent(@Param("userId") String userId, @Param("component") String component);

    @Select(SYS_MENU_DATA_RULES_VO_SQL +
            " AND sur.user_id = #{userId} " +
            " AND sm.dynamic_form_id=#{component} ")
    List<SysMenuDataRulesVo> listByUserIdAndDynamicFormId(@Param("userId") String userId, @Param("dynamicFormId") String dynamicFormId);

}
